Skip to content

fix: support batch broadcasting in JoinImageWithAlpha node#13309

Closed
Abdulrehman-PIAIC80387 wants to merge 1 commit intoComfy-Org:masterfrom
Abdulrehman-PIAIC80387:fix/join-image-alpha-batch-12580
Closed

fix: support batch broadcasting in JoinImageWithAlpha node#13309
Abdulrehman-PIAIC80387 wants to merge 1 commit intoComfy-Org:masterfrom
Abdulrehman-PIAIC80387:fix/join-image-alpha-batch-12580

Conversation

@Abdulrehman-PIAIC80387
Copy link
Copy Markdown
Contributor

Summary

Fixes #12580

Problem

The Join Image with Alpha node only returns a single image when given a batch of images with a single alpha mask.

Root cause: In comfy_extras/nodes_compositing.py line 205, the batch size was calculated using min(len(image), len(alpha)). When a user passes a batch of 4 images with a single alpha mask, min(4, 1) = 1, so only one image is produced.

This is inconsistent with every other compositing node in ComfyUI (e.g., ImageCompositeMasked), which correctly broadcasts the shorter input to match the longer one.

Fix

Changed comfy_extras/nodes_compositing.py to use max() instead of min() for batch size, with modulo indexing (i % len(...)) to repeat the shorter input:

# Before
batch_size = min(len(image), len(alpha))
...
out_images.append(torch.cat((image[i][:,:,:3], alpha[i].unsqueeze(2)), dim=2))

# After
batch_size = max(len(image), len(alpha))
...
out_images.append(torch.cat((image[i % len(image)][:,:,:3], alpha[i % len(alpha)].unsqueeze(2)), dim=2))

This handles all cases correctly:

  • 4 images + 1 mask → 4 output images (mask repeats)
  • 1 image + 4 masks → 4 output images (image repeats)
  • 4 images + 4 masks → 4 output images (1-to-1)
  • 1 image + 1 mask → 1 output image (unchanged behavior)

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 6, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 3a6fa03e-f239-4760-9538-afdb71179327

📥 Commits

Reviewing files that changed from the base of the PR and between bce9cd6 and 5c2ae6f.

📒 Files selected for processing (1)
  • comfy_extras/nodes_compositing.py
✅ Files skipped from review due to trivial changes (1)
  • comfy_extras/nodes_compositing.py

📝 Walkthrough

Walkthrough

The JoinImageWithAlpha.execute method's batch processing logic was changed to use max(len(image), len(alpha)) for batch size instead of min(...). The per-item loop now uses modulo-based indexing (image[i % len(image)], alpha[i % len(alpha)]) so smaller inputs repeat to match the larger batch. Alpha preprocessing (alpha = 1.0 - resize_mask(...)) and the final channel concatenation (torch.cat(..., dim=2)) remain unchanged.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding batch broadcasting support to the JoinImageWithAlpha node.
Description check ✅ Passed The description is directly related to the changeset, explaining the problem, root cause, and the fix with clear before/after code examples.
Linked Issues check ✅ Passed The PR fully addresses issue #12580 by implementing batch broadcasting with max() and modulo indexing, matching the expected behavior of other compositing nodes.
Out of Scope Changes check ✅ Passed All changes in comfy_extras/nodes_compositing.py are scoped to fixing the JoinImageWithAlpha batch broadcasting issue with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 6/8 reviews remaining, refill in 12 minutes and 8 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
comfy_extras/nodes_compositing.py (1)

205-210: Lock in the new broadcast behavior with a regression test.

This changes JoinImageWithAlpha from overlap-only batching to broadcast batching, so please add or update coverage for both N images + 1 mask and 1 image + N masks. tests-unit/comfy_extras_test/image_stitch_test.py:27-47 already shows the repo’s “larger batch wins” pattern for similar image ops.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@comfy_extras/nodes_compositing.py` around lines 205 - 210, JoinImageWithAlpha
now broadcasts batches (larger batch wins) so add a regression test that asserts
both broadcast cases: (a) N images + 1 mask and (b) 1 image + N masks produce
outputs with batch size equal to max(N_images, N_masks) and that the per-item
alpha channel matches the expected broadcasted mask values; locate the operator
under test (JoinImageWithAlpha in nodes_compositing.py, which uses resize_mask
and concatenates alpha) and follow the existing "larger batch wins" pattern from
the repo’s image-stitch tests to create two unit tests that construct small
synthetic tensors, run the operator, and assert output shape and channel content
for both scenarios.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@comfy_extras/nodes_compositing.py`:
- Around line 205-210: JoinImageWithAlpha now broadcasts batches (larger batch
wins) so add a regression test that asserts both broadcast cases: (a) N images +
1 mask and (b) 1 image + N masks produce outputs with batch size equal to
max(N_images, N_masks) and that the per-item alpha channel matches the expected
broadcasted mask values; locate the operator under test (JoinImageWithAlpha in
nodes_compositing.py, which uses resize_mask and concatenates alpha) and follow
the existing "larger batch wins" pattern from the repo’s image-stitch tests to
create two unit tests that construct small synthetic tensors, run the operator,
and assert output shape and channel content for both scenarios.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 18ced04d-feb6-46ff-9ad5-728129bdea09

📥 Commits

Reviewing files that changed from the base of the PR and between 4b1444f and bb5c337.

📒 Files selected for processing (1)
  • comfy_extras/nodes_compositing.py

@alexisrolland
Copy link
Copy Markdown
Member

@Abdulrehman-PIAIC80387 thank you for your contribution, this looks good.

Could you please address the error from the CI pipeline and update your PR?

Error: AI agent Co-authored-by trailers detected in PR commits.
The following commits contain Co-authored-by trailers from AI coding agents:
  bb5c3378: Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

These trailers should be removed before merging.
To fix, rewrite the commit messages with:
  git rebase -i d0f0b15cf5d1fbff67390c8d90ec8654c2582f7a

and remove the Co-authored-by lines, then force-push your branch.

Thanks

@Abdulrehman-PIAIC80387 Abdulrehman-PIAIC80387 force-pushed the fix/join-image-alpha-batch-12580 branch from 467b0c5 to bce9cd6 Compare May 3, 2026 12:09
@Abdulrehman-PIAIC80387
Copy link
Copy Markdown
Contributor Author

@alexisrolland Done — I removed the Co-authored-by trailer from the commit message and force-pushed. Thanks for the review!

Copy link
Copy Markdown
Member

@alexisrolland alexisrolland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and it works.

Image Image {B946D84F-EF1C-4770-A48D-8FF402DC9974}

@kijai
Copy link
Copy Markdown
Collaborator

kijai commented May 3, 2026

Hey, thank you for raising the issue and for the suggested fix.

However I would rather do this using the existing comfy.utils.repeat_to_batch_size helper for the purpose, which handles the error case here correctly already, so I made separate PR for that: #13686

@alexisrolland
Copy link
Copy Markdown
Member

Agree with @kijai the comfy.utils.repeat_to_batch_size is more elegant so I'll close this one. Thanks for your contribution @Abdulrehman-PIAIC80387 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Join Image With Alpha node does not work with image batching

3 participants